package com.sinba.common.business.dao;


import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.sinba.common.business.dto.MatchQueryDTO;
import com.sinba.common.business.entity.Match;
import com.sinba.common.dao.BaseDAOImplHibernate;
import com.sinba.common.dto.PageDTO;
import com.sinba.common.util.StringUtils;

public class MatchDAO extends BaseDAOImplHibernate<Match> implements IMatchDAO{
	public PageDTO findMatchPage(MatchQueryDTO queryDTO,String sord,String sidx){
		
		final DetachedCriteria dc = DetachedCriteria.forClass(Match.class);
		
		if(queryDTO.getId()!=null){
			dc.add(Restrictions.eq("id", queryDTO.getId()));
		}
		
		
		//买入会员
		if(StringUtils.hasText(queryDTO.getBuyUser()) || (queryDTO.getBuyId()!=null && queryDTO.getBuyId()>0)){
			dc.createAlias("buy", "buy");
		}
		if(StringUtils.hasText(queryDTO.getBuyUser())){
			
			dc.add(Restrictions.eq("buy.buyUser", queryDTO.getBuyUser()));
		}
		if(queryDTO.getBuyId()!=null && queryDTO.getBuyId()>0){
			dc.add(Restrictions.eq("buy.id", queryDTO.getBuyId()));
		}
		
		
		//卖出会员
		if(StringUtils.hasText(queryDTO.getSellUser()) || (queryDTO.getSellId()!=null && queryDTO.getSellId()>0)){
			dc.createAlias("sell", "sell");
		}
		
		if(StringUtils.hasText(queryDTO.getSellUser())){
			dc.add(Restrictions.eq("sell.sellUser", queryDTO.getSellUser()));
		}
		if(queryDTO.getSellId()!=null && queryDTO.getSellId()>0){
			dc.add(Restrictions.eq("sell.id", queryDTO.getSellId()));
		}
		
		if ( queryDTO.getStatus() != null) {
			dc.add(Restrictions.eq("status", queryDTO.getStatus()));
		}
		
		if(queryDTO.getMatchStates()!=null && queryDTO.getMatchStates().length>0){
			dc.add(Restrictions.in("status", queryDTO.getMatchStates()));
		}
		
		
		if(MatchQueryDTO.FindNoCalculat.equals(queryDTO.getSearchType())){
			dc.add(Restrictions.eq("isCalculated", false));
		}
		
		
		if ( ( sord != null ) && ! sord.equals( "" ) && ( sidx != null ) && ! sidx.equals( "" ) ){
            if ( sord.equals( "desc" ) )
            {
                dc.addOrder( Order.desc( sidx ) );
            } else{
                dc.addOrder( Order.asc( sidx ) );
            }
        }
		return super.findPageByCriteria( dc,queryDTO.getStart(  ),queryDTO.getLimit(  ) );
		
	};
	
	
	public List<Match> findTimeOutMatch(int timeoutLong,Integer[] status,String field){
		
		final DetachedCriteria dc = DetachedCriteria.forClass(Match.class);
		dc.add(Restrictions.in("status", status));
		dc.createAlias("buy", "buy");
		dc.createAlias("buy.user", "buyUser");
		dc.add(Restrictions.or(Restrictions.isNull("buyUser.userStatus"), Restrictions.eq("buyUser.userStatus", "0")));
		//排队6天后的订单
		Calendar timeCl = new GregorianCalendar(  );
		timeCl.setTime( new Date(  ) );
		timeCl.add(Calendar.HOUR_OF_DAY, -timeoutLong);
		dc.add(Restrictions.le(field, timeCl.getTime()));
		return super.getHibernateTemplate().findByCriteria(dc);
	}
	
	
}
